package org.jash.attendance.model

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.attendance.net.SignService
import org.jash.common.mvi.BaseState
import org.jash.common.mvi.BaseViewModel
import org.jash.common.net.retrofit

class SignViewModel:BaseViewModel<SignIntent, SignState>() {
    val service = retrofit.create(SignService::class.java)
    fun selSign(selSign:SignIntent.SelSign) {
        viewModelScope.launch {
            state.value = try {
                val res =
                    service.selSign(mapOf("userId" to selSign.userId, "page" to selSign.page))
                if (res.statusCode == 200) {
                    SignState.SignList(res.data)
                } else {
                    BaseState.Error(res.msg)
                }
            } catch (e:Exception) {
                BaseState.Error(e.message ?: "")
            }
        }
    }
    fun sign(sign:SignIntent.Sign) {
        viewModelScope.launch {
            state.value = try {
                val res =
                    service.sign(sign.signInfo)
                SignState.SignResponse(res.statusCode == 200)
            } catch (e:Exception) {
                BaseState.Error(e.message ?: "")
            }
        }
    }
}